//index.js
//获取应用实例
const app = getApp()

var process_def = require("./../process-definition/process-definition");

Page({
  data: {
    proccess_name : "index_unlock_process",
    isLogin : true, //是否登录标识 false：未登录； true：已登录 (测试使用，实际是调用后台接口，查询登录情况)
    isThrowError : false, //是否出现不可捕获异常（例如1/0这种不可捕获的异常）
  },
  
  onLoad: function () {
    this.isLogin = true;
    this.isThrowError = false;
  },


  //初始化流程
  initProcess: function() {
    process_def.addProcess(this.proccess_name)
    .dev()
    .finally(this.finalize)
    .add(this.loginCheck, "登录校验")
    .add_delay()
    .add(this.connectBle, "连接蓝牙")
    .add_pre(this.connectBlePre) //数据预处理
    .add_err(this.connectBleErr)
    .add(this.unLock, "开锁")
    .add(this.disConnectBle, "断开蓝牙");
  },
  startProcess: function() {
    var process = process_def.getProcess(this.proccess_name);
    if (typeof process == undefined || process == undefined) {
      this.initProcess();
      process = process_def.getProcess(this.proccess_name);
    }
    if (process.get_state()) {
      console.log("当前流程在正在运行，请稍后再试");
    } else {
      process.start();
    }
  },

  stopProcess() {
    process_def.getProcess(this.proccess_name).stop();
  },
  normalStart() {
    this.startProcess();

  },
  LoginFailTest() {
    this.isLogin = false;
    this.startProcess();
  },

  throwErrorTest() {
    this.isThrowError = true;
    this.startProcess();
  },

  /**
   * 流程具体方法定义
   * 假设该流程实现开锁任务, 函数功能和顺序如下：
   * 1. loginCheck : 判断用户是否登录(如果登录了,则执行下一个方法，反之，则触发登录的拒绝策略)
   * 2. connectBle : 连接蓝牙(连接蓝牙，如果连接成功，则执行下一个方法。 反之，则触发连接蓝牙的拒绝策略)
   * 3. unLock : 蓝牙开锁(使用蓝牙进行开锁， 如果开锁成功, 则执行下一个方法。 反之，则触发开锁的拒绝策略)
   * 4. disConnectBle: 断开蓝牙
   */
  loginCheck: function() {
    if (this.isLogin) {
      process_def.getProcess(this.proccess_name).log("登录成功");
      process_def.getProcess(this.proccess_name).then();
    } else {
      process_def.getProcess(this.proccess_name).log("登录失败");
      process_def.getProcess(this.proccess_name).reject("该用户未登录");
    }
  }, 
  loginCheckRject(res) {
    process_def.getProcess(this.proccess_name).log("打印失败信息:" + res);
  },
  connectBle: function(times) {
    if (times < 3 ) {
      process_def.getProcess(this.proccess_name).log("连接失败，重试第" + times + "次");
      return this.connectBle(++times);
    }
    if (this.isThrowError) {
      throw new Error("蓝牙连接失败，停止流程");
    } else {
      process_def.getProcess(this.proccess_name).log("连接成功");
      process_def.getProcess(this.proccess_name).then();
    }
  }, 
  //数据预处理，设置连接蓝牙函数connectBle的入参(第一次连接故返回1)
  connectBlePre : function() {
    return 1;
  },
  connectBleErr : function() {
    process_def.getProcess(this.proccess_name).log("连接蓝牙异常，触发《蓝牙连接异常函数》");
  },
  unLock: function() {
    process_def.getProcess(this.proccess_name).log("开锁成功");
    process_def.getProcess(this.proccess_name).then();
  }, 
  disConnectBle: function() {
    process_def.getProcess(this.proccess_name).log("蓝牙断开成功");
    process_def.getProcess(this.proccess_name).then();
  }, 
  finalize :function() {
    //流程结束，卸载数据(数据重置)
    this.isLogin = true;
    this.isThrowError = false;
  }
  
})
